<?php

namespace ticky;

/**
 *  文章采集类
 */
class collection {

    public static $url;

    /**
     * 获取目标网址HTML源码
     * @param $url 目标网址url
     * @return string
     */
    public static function get_content($url) {
        self::$url = $url;
        $request_url_hash = md5($url);
        $request_url_cache_file = sprintf("./runtime/temp/%s.url", $request_url_hash);
        $source = '';
        // 缓存请求数据，避免重复请求
        if (file_exists($request_url_cache_file) && (time() - filemtime($request_url_cache_file) < 3600)) {
            $source = file_get_contents($request_url_cache_file);
        } else {
            $user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            if (function_exists('curl_init')) {
                $curl_handle = curl_init();
                curl_setopt($curl_handle, CURLOPT_URL, $url);
                curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
                curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($curl_handle, CURLOPT_FAILONERROR, 1);
                curl_setopt($curl_handle, CURLOPT_TIMEOUT, 2);
                curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
                $source = curl_exec($curl_handle);
                $encode = mb_detect_encoding($source, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5"));
                if ($encode != "UTF-8") {
                    $source = mb_convert_encoding($source, "UTF-8", $encode);
                }
                curl_close($curl_handle);
            } else {
                $source = '';
            }
            $source = preg_replace("/<br\/?>[ \r\n\s]*<br\/?>/i", "</p><p>", $source);
            $source = preg_replace("#<script(.*?)>(.*?)</script>#is", "", $source); //过滤script标签
            $source = preg_replace("/<!--.*?-->/si", "", $source); //注释
            $source = preg_replace("@<style(.*?)</style>@is", "", $source); //过滤style标签
            $source = preg_replace("/<(\/?link.*?)>/si", "", $source); //过滤link标签
            $source = preg_replace("/<(\/?meta.*?)>/si", "", $source); //过滤meta标签
            //debug($source);
            if (!empty($source)) {
                if ($fp = @fopen($request_url_cache_file, 'a')) {
                    @flock($fp, 2);
                    fwrite($fp, '<?xml encoding="utf-8">' . $source);
                    fclose($fp);
                }
            } else {
                runlog('cj', 'get_content:' . $url);
            }
        }
        return trim($source);
    }

    /**
     * 获取区间内的HTML源码
     * @param $html 目标网址的HTML源码
     * @param $start 区间开始的html标识
     * @param $end 区间结束的html标识
     * @return string
     */
    public static function get_sub_content($html, $start, $end) {
        if (empty($html))
            return '';
        if ($start == '' || $end == '') {
            return $html;
        }
        $html = str_replace(array("\r", "\n"), "", $html);
        $start = str_replace(array("\r", "\n"), "", $start);
        $end = str_replace(array("\r", "\n"), "", $end);
        $html = explode(trim($start), $html);
        if (is_array($html))
            $html = explode(trim($end), $html[1]);

        return trim($html[0]);
    }

    /**
     * 根据区间内的HTML源码，提取文章的URL和TITLE
     * @param $html 区间内的HTML源码
     * @param $url_contain 网址中必须包含
     * @param $url_except 网址中不能包含
     * @return array
     */
    public static function get_all_url($html, $url_contain = '', $url_except = '') {
        $html = str_replace(array("\r", "\n"), '', $html);
        $html = str_replace(array("</a>", "</A>"), "</a>\n", $html);
        preg_match_all('/<a ([^>]*)>([^\/a>].*)<\/a>/i', $html, $out);
        $data = array();
        foreach ($out[1] as $k => $v) {
            if (preg_match('/href=[\'"]?([^\'" ]*)[\'"]?/i', $v, $match_out)) {
                if ($url_contain) {
                    if (strpos($match_out[1], $url_contain) === false) {
                        continue;
                    }
                }
                if ($url_except) {
                    if (strpos($match_out[1], $url_except) !== false) {
                        continue;
                    }
                }
                $url2 = $match_out[1];
                $url2 = self::url_check($url2, self::$url);
                $title = strip_tags($out[2][$k]);
                if (empty($url2) || empty($title))
                    continue;
                $data['url'][$k] = trim($url2);
                $data['title'][$k] = trim($title);
            } else {
                continue;
            }
        }
        //去除重复数据
        $arr = array();
        $data['url'] = array_unique($data['url']);
        foreach ($data['url'] as $k => $v) {
            $arr['url'][] = $data['url'][$k];
            $arr['title'][] = $data['title'][$k];
        }
        return $arr;
    }

    /**
     * 根据文章内容页的HTML源码，过滤并提取相关信息
     * @param $html 文章内容页面的HTML源码
     * @return array
     */
    public static function get_filter_html($html, $config = array()) {
        $data = array();
        //获取内容
        $data['title'] = self::replace_item(self::get_sub_content($html, $config['title_rule'][0], $config['title_rule'][1]), $config['title_html_rule']);
        //获取时间
        if ($config['time_rule'][0] != '' && $config['time_rule'][1] != '') {
            $data['inputtime'] = self::replace_item(self::get_sub_content($html, $config['time_rule'][0], $config['time_rule'][1]), $config['time_html_rule']);
            $data['inputtime'] = !empty($data['inputtime']) ? strtotime($data['inputtime']) : TIMESTAMP;
        } else {
            $data['inputtime'] = TIMESTAMP;
        }
        //获取内容
        $content = self::get_sub_content($html, $config['content_rule'][0], $config['content_rule'][1]);
        $data['content'] = self::replace_item($content, $config['content_html_rule']);

        return $data;
    }

    /**
     * 根据配置项，切割数据
     * @param $separator 以什么字符分割字符串
     * @param $string 要处理的字符串
     * @return array
     */
    public static function myexp($separator, $string) {
        if (empty($string))
            return array('', '');
        $string = str_replace(array("\r", "\n"), '', $string);
        $arr = explode($separator, $string);
        if ($arr[count($arr) - 1] == '')
            unset($arr[count($arr) - 1]);
        return $arr;
    }

    /**
     * 过滤代码
     * @param string $html  HTML代码
     * @param array $config 过滤配置
     * @return string
     */
    protected static function replace_item($html, $config) {
        if (!is_array($config) || empty($config))
            return $html;
        $patterns = $replace = array();
        foreach ($config as $k => $v) {
            $patterns[] = '/' . str_replace('/', '\/', $v) . '/i';
            $replace[] = '';
        }
        return trim(preg_replace($patterns, $replace, $html));
    }

    /**
     * URL地址检查
     * @param string $url      需要检查的URL
     * @param string $baseurl  基本URL
     * @return string
     */
    protected static function url_check($url, $baseurl) {
        $urlinfo = parse_url($baseurl);
        $baseurl = $urlinfo['scheme'] . '://' . $urlinfo['host'] . (substr($urlinfo['path'], -1, 1) === '/' ? substr($urlinfo['path'], 0, -1) : str_replace('\\', '/', dirname($urlinfo['path']))) . '/';
        if (strpos($url, '://') === false) {
            if ($url[0] == '/') {
                $url = $urlinfo['scheme'] . '://' . $urlinfo['host'] . $url;
            } else {
                $url = $baseurl . $url;
            }
        }
        return $url;
    }

}
